<?PHP
include_once ("../dbconfig.php");

set_time_limit(180);

class lsServer {
	private $link;
	function cleanUploadBuf($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$dir = opendir('./uploadbuf/');
			while ($file = readdir($dir)) {
				if ($file != "." && $file != "..") {
					if ($file != "image_source" && $file != "thumb") {
						unlink('./uploadbuf/' . $file);
					}
				}
			}
			$dir2 = opendir('./uploadbuf/thumb/');
			while ($file2 = readdir($dir2)) {
				if ($file2 != "." && $file2 != "..") {
					unlink('./uploadbuf/thumb/' . $file2);
				}
			}
			$no_use_image = $this->findOutLinkImage();
			if ($no_use_image) {
				for ($j = 0; $j < count($no_use_image); $j++) {
					$query = "delete from imagelist where id=" . intval($no_use_image[$j]['id']);
					$this->queryDB($query, true);
					$this->deleteFile($inputObj['id'], $inputObj['psw'], './images/thumb/' . $no_use_image[$j]['image_path']);
					$this->deleteFile($inputObj['id'], $inputObj['psw'], './images/' . $no_use_image[$j]['image_path']);
				}
			}
			return true;
		} else {
			return false;
		}
	}
	function checkLog($id, $psw) {
		$query = "select * from administrator where id='" . $id . "' and psw=md5('" . $psw . "')";
		$result = $this->queryDB($query);
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	function addColumnItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "insert into deschtml values(Null,'" . addslashes($inputObj['name']) . "','" . addslashes($inputObj['content']) . "','" . addslashes($inputObj['active']) . "','" . addslashes($inputObj['owner']) . "','" . addslashes($inputObj['isone']). "',Null,Null,Null,'". addslashes($inputObj['imgrangetype']) . "','". addslashes($inputObj['lang']) . "')";
			$this->queryDB($query, true);
			return mysqli_insert_id($this->link);
		} else {
			return false;
		}
	}
	function updateColumnItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "update deschtml set name='" . addslashes($inputObj['name']) . "',owner='" . addslashes($inputObj['owner']) . "',context='" . addslashes($inputObj['content']) . "',active='" . addslashes($inputObj['active']) . "',isone='" . addslashes($inputObj['isone']) . "' where id=" . $inputObj['itemid'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function deleteColumnItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "delete from deschtml where id=" . $inputObj['itemid'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function addNewsItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "insert into news values(Null,'" . addslashes($inputObj['title']) . "','" . addslashes($inputObj['content']) . "','" . addslashes($inputObj['startdate']) . "','" . addslashes($inputObj['enddate']) . "','" . addslashes($inputObj['active']). "','" . addslashes($inputObj['lang']). "','" . addslashes($inputObj['imgname']). "','" . addslashes($inputObj['imgurl']) . "')";
			$this->queryDB($query, true);
			return mysqli_insert_id($this->link);
		} else {
			return false;
		}
	}
	function updateNewsItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "update news set title='" . addslashes($inputObj['title']) . "',content='" . addslashes($inputObj['content']) . "',startdate='" . addslashes($inputObj['startdate']) . "',enddate='" . addslashes($inputObj['enddate']) . "',active='" . addslashes($inputObj['active']) . "',imgname='" . addslashes($inputObj['imgname']) . "',imgurl='" . addslashes($inputObj['imgurl']) . "' where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function deleteNewsItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "delete from news where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function getLevel1Menu($inputObj) {
		$query = "select * from deschtml where isone='1' and lang='".$inputObj['lang']."' order by id desc";
		$result = $this->queryDB($query);
		return $result;
	}
	function getLevel2Menu($inputObj) {
		$query = "select * from deschtml where owner='" . addslashes($inputObj['levelid']) . "' order by id desc";
		$result = $this->queryDB($query);
		return $result;
	}

	function getDescHtml($htmlId) {
		$query = "select * from deschtml where id='" . addslashes($htmlId) . "'";
		$result = $this->queryDB($query);
		return $result[0];
	}
	function updateDescHtml($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "update deschtml set context='" . addslashes($inputObj['context']) . "',title='" . addslashes($inputObj['title']) . "',keyword='" . addslashes($inputObj['keyword']) . "',searchdesc='" . addslashes($inputObj['desc']). "',imgrangetype='" . addslashes($inputObj['imgrangetype']) . "' where id=" . $inputObj['htmlId'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function getNewsItems($lang) {
		$query = "select * from news where lang=".$lang." order by id desc";
		$result = $this->queryDB($query);
		return $result;
	}
	function getCustomerLogosItems() {
		$query = "select * from customerlogo order by id desc";
		$result = $this->queryDB($query);
		return $result;
	}
	function addCustomerLogoItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "insert into customerlogo values(Null,'" . addslashes($inputObj['name']) . "','" . addslashes($inputObj['link']) . "','" . addslashes($inputObj['url']) . "','" . addslashes($inputObj['active']) . "')";
			$this->queryDB($query, true);
			return mysqli_insert_id($this->link);
		} else {
			return false;
		}
	}
	function updateCustomerLogoItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "update customerlogo set name='" . addslashes($inputObj['name']) . "',link='" . addslashes($inputObj['link']) . "',url='" . addslashes($inputObj['url']) . "',active='" . addslashes($inputObj['active']) . "' where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function deleteCustomerLogoItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "delete from customerlogo where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function getProImgItems($owner) {
		$query = "select * from proimages where owner='".addslashes($owner)."' order by id desc";
		$result = $this->queryDB($query);
		return $result;
	}
	function addProImgItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "insert into proimages values(Null,'" . addslashes($inputObj['name']) . "','" . addslashes($inputObj['url']) . "','" .addslashes($inputObj['owner']) . "','"  . addslashes($inputObj['active']). "','"  . addslashes($inputObj['lang']) . "')";
			$this->queryDB($query, true);
			return mysqli_insert_id($this->link);
		} else {
			return false;
		}
	}
	function updateProImgItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "update proimages set name='" . addslashes($inputObj['name']) . "',owner='" . addslashes($inputObj['owner']) . "',url='" . addslashes($inputObj['url']) . "',active='" . addslashes($inputObj['active']) . "' where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function deleteProImgItem($inputObj) {
		if ($this->checkLog($inputObj['id'], $inputObj['psw'])) {
			$query = "delete from proimages where id=" . $inputObj['news_id'];
			$this->queryDB($query, true);
			return true;
		} else {
			return false;
		}
	}
	function getUploadSize() {
		$maxsize = ini_get('upload_max_filesize');
		if (!is_numeric($maxsize)) {
			if (strpos($maxsize, 'M') !== false)
				$maxsize = intval($maxsize) * 1024 * 1024;
			elseif (strpos($maxsize, 'K') !== false) $maxsize = intval($maxsize) * 1024;
			elseif (strpos($maxsize, 'G') !== false) $maxsize = intval($maxsize) * 1024 * 1024 * 1024;
		}
		return $maxsize;
	}
	private function queryDB($query, $sourceRe = false) {
		$this->link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
		$result = mysqli_query($this->link, $query);
		if ($sourceRe) {
			return $result;
		} else {
			$re = array ();
			$num_results = $result->num_rows;
			if (is_int($num_results) && $num_results > 0) {
				for ($i = 0; $i < $num_results; $i++) {
					$row = $result->fetch_assoc();
					$re[$i] = $row;
				}
				return $re;
			} else {
				return false;
			}
		}
	}
}
?>